FileName           := STACK_FRAME
BFileSource           := IN.ASM
BFileExe           := IN.EXE

CC                 := ML.EXE
LD                 := ML.EXE
GDB                := CV.EXE

DISC_INSTRUMENTS   := A:
DISC_BUILDS        := B:
DISC_OUTPUT        := C:

CurrentDir         := $(shell pwd -P)
FullPathOutPut_TTY := $(shell tty)
OutPutFile_TTY     := $(shell basename ${FullPathOutPut_TTY})
OutPutDir_TTY      := $(FullPathOutPut_TTY:%/$(OutPutFile_TTY)=%)

FullPathOutPut_FILE := $(CurrentDir)/OUT
OutPutFile_FILE     := $(shell basename ${FullPathOutPut_FILE})
OutPutDir_FILE      := $(FullPathOutPut_FILE:%/$(OutPutFile_FILE)=%)

ASM_DIR            := ~/.local/opt/dosbox-masm/C/MASM611/BIN/
BUILD_DIR          := $(CurrentDir:%=%/build)
SRC_DIR            := $(CurrentDir:%=%/src)

OBJ                := ${FileName:%=%.OBJ}
SRC                := ${FileName:%=%.ASM}
FileType           := .EXE

OUT := $(DISC_OUTPUT)/$(OutPutFile_TTY)
OUT_FILE := $(DISC_OUTPUT)/$(OutPutFile_FILE)

all: compile_and_exit_out_in_file

compile_and_exit_out_in_file: check_build
	cp $(SRC_DIR)/$(SRC) $(BUILD_DIR)/$(BFileSource)
	dosbox                                                     \
    -c "mount $(DISC_INSTRUMENTS) $(ASM_DIR)"                \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "mount $(DISC_OUTPUT) $(OutPutDir_FILE)"               \
    -c "$(DISC_BUILDS)"                                      \
    -c "PATH $(DISC_INSTRUMENTS)/"                           \
    -c "$(CC) $(BFileSource) >>$(OUT_FILE)"\
    -c exit >> /dev/null
	cat $(FullPathOutPut_FILE)
	rm $(FullPathOutPut_FILE) $(BUILD_DIR)/$(BFileSource)

compile_and_exit: check_build
	cp $(SRC_DIR)/$(SRC) $(BUILD_DIR)/$(BFileSource)
	dosbox                                                     \
    -c "mount $(DISC_INSTRUMENTS) $(ASM_DIR)"                \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "mount $(DISC_OUTPUT) $(OutPutDir_TTY)"               \
    -c "$(DISC_BUILDS)"                                      \
    -c "PATH $(DISC_INSTRUMENTS)/"                           \
    -c "$(CC) $(SRC) >>$(OUT)"\
    -c exit >> /dev/null



compile_without_exit: check_build
	cp $(SRC_DIR)/$(SRC) $(BUILD_DIR)/$(BFileSource)
	dosbox                                                   \
    -c "mount $(DISC_INSTRUMENTS) $(ASM_DIR)"                \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "mount $(DISC_OUTPUT) $(OutPutDir_TTY)"               \
    -c "$(DISC_BUILDS)"                                      \
    -c "PATH $(DISC_INSTRUMENTS)/"                           \
    -c "$(CC) $(BFileSource)"                                  \
    >> /dev/null
	rm $(FullPathOutPut_FILE) $(BUILD_DIR)/$(BFileSource)
    
run: run_out_in_file

run_out_in_file: $(BUILD_DIR)/$(BFileExe)
	dosbox                                                   \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "mount $(DISC_OUTPUT) $(OutPutDir_FILE)"               \
    -c "PATH $(DISC_BUILDS)/"                                \
    -c "$(BFileExe) >>$(OUT_FILE)"  \
    -c exit >> /dev/null
	cat $(FullPathOutPut_FILE)
	wc -l $(FullPathOutPut_FILE)
	rm $(FullPathOutPut_FILE)

run_out_in_tty: $(BUILD_DIR)/$(BFileExe)
	dosbox                                                   \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "mount $(DISC_OUTPUT) $(OutPutDir_TTY)"               \
    -c "PATH $(DISC_BUILDS)/"                                \
    -c "$(BFileExe) >>$(OUT)"  \
    -c exit >> /dev/null

gdb: $(BUILD_DIR)/$(BFileExe)
	dosbox                                                   \
    -c "mount $(DISC_INSTRUMENTS) $(ASM_DIR)"                \
    -c "mount $(DISC_BUILDS) $(BUILD_DIR)"                   \
    -c "$(DISC_BUILDS)"                                      \
    -c "PATH $(DISC_INSTRUMENTS)/;$(DISC_BUILDS)/"           \
    -c "$(GDB) $(BFileExe)"                              \
    -c exit >> /dev/null

clear:
	rm -fr $(BUILD_DIR)/$(BFileExe)

check_build:
	if [ ! -d "$(BUILD_DIR)" ]; then mkdir $(BUILD_DIR); fi
